package org.lab.metricmodel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.lab.bean.ModelBean;
import org.lab.service.UrlForward;
import org.lab.util.BeanUtils;


/**页面的逻辑控制
 * @author 李晓娜
 *
 */
public class metricmessage extends HttpServlet {
	private Logger log = Logger.getLogger(metricmessage.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		req.setCharacterEncoding("GB2312");
		resp.setCharacterEncoding("GB2312");
		resp.setContentType("text/html;charset=GB2312");
		
		ModelBean bean = null;
		if(req.getSession().getAttribute("modelBean")==null){
			bean = new ModelBean();
		}else{
			bean = (ModelBean)req.getSession().getAttribute("modelBean");
		}
		
		BeanUtils.copyProperties(req, bean);
		
		req.getSession().setAttribute("modelBean", bean);
		
		
		UrlForward url = new UrlForward();
		
		//获取导向资源文件
		ResourceBundle resource = ResourceBundle.getBundle("forward");
		
	
		
		/**
		 * 解析xml所需参数
		 * */
		String para1 = "";
		String para = "";
		if (req.getParameter("para") != null) {
			para1 = req.getParameter("para");
			para = new String(para1.getBytes("ISO-8859-1"),"GB2312");
		}
		log.info("para1 " + para1);
		log.info("para " + para);
		log.info("页面page标记："+ bean.getPage());
		
		//度量方向页面转向
		if("classify".equals(bean.getPage()) && url.forward(resource.getString(bean.getPage()), req, resp)){
			log.info("classify跳转，classify值为："+bean.getInstances());
			return;
		}
		//基度量元页面跳转
		
		if("metricnameb".equals(bean.getPage()) && url.forward(resource.getString("metricnameb"), req, resp)){
			//log.info("baseobject 中的值：" + new String(bean.getBaseobject().getBytes("iso-8859-1"),"GB2312"));
			log.info("bdescription 中的值：" + bean.getBdescription());
			log.info("进入 metricobj.jsp 页面");
			return;
		}
		//度量对象页面跳转到度量信息收集状况页面
		if("metricobj".equals(bean.getPage()) && url.forward(resource.getString("metricobj"), req, resp)){
			//log.info("baseobject 中的值：" + new String(bean.getBaseobject().getBytes("iso-8859-1"),"GB2312"));
			log.info("metricinfo 中的值：" + bean.getMetricinfo());
			log.info("进入 condition.jsp 页面");
			return;
		}
		//多实例度量计算条件页面转向度量信息收集频率页面
		
		if("condition".equals(bean.getPage()) && url.forward(resource.getString("condition"), req, resp)){
			//log.info("condition 中的值：" + new String(bean.getCondition().getBytes("iso-8859-1"),"GB2312"));
			log.info("starttime中的值：" + bean.getStartime());
			log.info("endtime中的值：" + bean.getEndtime());
			log.info("degree中的值：" + bean.getDegree());
			log.info("进入metricFrequency.jsp");
			return;
		}
		//派生度量元页面跳转
		
		if("metricnamed".equals(bean.getPage()) && url.forward(resource.getString("metricnamed"), req, resp)){
			//log.info("deriveobject 中的值：" + new String(bean.getDeriveobject().getBytes("iso-8859-1"),"GB2312"));
			log.info("ddescription 中的值：" + bean.getDdescription());
			log.info("进入 BaseMetric.jsp 页面");
			return;
		}
		//度量对象类型页面转向
		if("metricObjType".equals(bean.getPage()) && url.forward(resource.getString(bean.getProcess()), req, resp)){
			log.info("process跳转，process值为："+bean.getProcess());
			return;
		}
		//派生度量度量元页面转向
		
		if("metricName".equals(bean.getPage()) && url.forward(resource.getString("metricname"), req, resp)){
			log.info("metricname 中的值：" + new String(bean.getMetricname().getBytes("iso-8859-1"),"GB2312"));
			log.info("进入metricFrequency");
			return;
		}
		//活动度量对象页面转向
		
		if("metricObj_Activity".equals(bean.getPage()) && bean.getObject() != null){
			resp.sendRedirect(resource.getString("object"));
			log.info("object中的值："+new String(bean.getObject().getBytes("iso-8859-1"),"GB2312"));
			log.info("进入metricType");
			return;
		}
		
		//度量频率页面转向
		
		if("metricfrequency".equals(bean.getPage()) && url.forward(resource.getString("metricfrequency"), req, resp)){
			log.info("metricfrequency 中的值：" + bean.getFrequency());
			log.info("metricfrequency 中的值：" + bean.getFrequencys());
			log.info("hour:"+ bean.getHour());
			log.info("minute:"+bean.getMinute());
			log.info("second:"+bean.getSecond());
			log.info("进入Statistic.jsp");
			return;
		}
		//子流程度量对象页面转向
		
		if("seobject".equals(bean.getPage()) && url.forward(resource.getString("mOsubprocess"), req, resp)){
			log.info("startobject 中的值：" + bean.getStartobject());
			log.info("endobject 中的值：" + bean.getEndobject());
			log.info("进入metricType.jsp");
			return;
		}
		//多实例度量元选择页面转向
		
		if("multimetric".equals(bean.getPage()) && url.forward(resource.getString("multimetric"), req, resp)){
			log.info("musable 中的值：" + new String(bean.getMusable().getBytes("iso-8859-1"),"GB2312"));
			log.info("进入MultiFunction.jsp");
			return;
		}
		//多实例度量元选择页面转向
		
		if("mfunction".equals(bean.getPage()) && url.forward(resource.getString("mfunction"), req, resp)){
			//log.info("mfunction 中的值：" + new String(bean.getMfunction().getBytes("iso-8859-1"),"GB2312"));
			log.info("mformula中的值：" + bean.getMformula());
			log.info("defineself中的值：" + bean.getDefineself());
			log.info("进入condition.jsp");
			return;
		}
		
		//多实例度量处理页面转向
		
		if("treatment".equals(bean.getPage()) && url.forward(resource.getString("treatment"), req, resp)){
			//log.info("treatment 中的值：" + new String(bean.getTreatment().getBytes("iso-8859-1"),"GB2312"));
			log.info("serial中的值：" + bean.getStartime());
			log.info("compute中的值：" + bean.getEndtime());
			log.info("进入displayAndalarm.jsp");
			return;
		}
		//进行基度量和派生度量选择的页面跳转
		
		if("metricyuantype".equals(bean.getPage()) && url.forward(resource.getString(bean.getMetric()), req, resp)){
			//log.info("metric 中的值：" + new String(bean.getMetric().getBytes("iso-8859-1"),"GB2312"));
			log.info("metricyuantype 跳转");
			return;
		}
		
		
		//派生度量基度量元选择页面跳转
		
		if("basemetric".equals(bean.getPage()) && url.forward(resource.getString("basemetric"), req, resp)){
			log.info("suseful 中的值：" + bean.getSusable());
			log.info("进入 Function.jsp 页面");
			return;
		}
		//派生度量函数选择页面跳转
		
		if("function".equals(bean.getPage()) && url.forward(resource.getString("function"), req, resp)){
			log.info("formula 中的值：" + bean.getFormula());
			log.info("deffunction 中的值：" + bean.getDeffunction());
			log.info("进入 metricFrequency.jsp 页面");
			return;
		}
		//转向最终显示页面
		
		if("displayandalarm".equals(bean.getPage()) && url.forward(resource.getString("displayandalarm"), req, resp)){
			log.info("display 中的值：" + bean.getDisplay());
			//log.info("floatarea 中的值：" + bean.getFloatarea());
			//log.info("centesimal 中的值：" + bean.getCentesimal());
			log.info("material 中的值：" + bean.getMaterial());
			log.info("material2 中的值：" + bean.getMaterial2());
			log.info("alarm 中的值：" + bean.getAlarm());
			log.info("进入 display.jsp 页面");
			return;
		}
		//最终默认
		url.forward(resource.getString("error")+bean.getPara(), req, resp);
		
		log.info("最终默认error");
		
		
		return;
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
		

	}
	
}
